package com.app.freshmenapp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.app.freshmenapp.entity.News;
import com.app.freshmenapp.service.NewsService;

@RestController
@RequestMapping("/news")
public class NewsController {

	@Autowired
	NewsService newsService;
	
	@RequestMapping(value = "/list")
	public Map<String,Object> findAllNews() {
		List<News> newsList = newsService.findAllNews();
		HashMap<String, Object> map = new HashMap<>();
		map.put("success", true);
		map.put("msg", "成功");
		map.put("list", newsList);
		return map;
	}
	@PostMapping("/findone/{id}")
	public Map<String, Object> findNewsRestful(@PathVariable("id") Integer id) {
		HashMap<String, Object> map = new HashMap<String, Object> ();
		map.put("success",false);
		map.put("msg", "失败");
		News news = newsService.findNews(id);
		if(news!=null) {
			map.put("success",true);
			map.put("msg", "成功");
			map.put("data", news);
		}
		return map;
	}
	@PostMapping("/deleteone/{id}")
	public Map<String,Object> deleteNewsRestful(@PathVariable("id") Integer id) {
		HashMap<String, Object> map = new HashMap<String, Object> ();
		map.put("success",false);
		map.put("msg", "失败");
		boolean result = newsService.deleteNews(id);
		if(result==true) {
			newsService.deleteNews(id);
			map.put("success",true);
			map.put("msg", "成功");
		}
		return map;
	}
	@PostMapping("/add")
	public Map<String,Object> addNewsRestful(@RequestBody News news) {
		HashMap<String, Object> map = new HashMap<String, Object> ();
		boolean result  = newsService.addNews(news);
		if(result == true) {
			map.put("success",true);
			map.put("msg", "成功");
		}else {
			map.put("success",false);
			map.put("msg", "失败");
		}
		return map;
	}
	// == @RequestMapping(value="/updateone",method=RequestMethod.PUT)
	@PostMapping("/updateone")
	public Map<String,Object> updateNewsRestful(@RequestBody  News news) {
		HashMap<String, Object> map = new HashMap<String, Object> ();
		boolean result  = newsService.updateNews(news);;
		if(result == true) {
			map.put("success",true);
			map.put("msg", "成功");
		}else {
			map.put("success",false);
			map.put("msg", "失败");
		}
		return map;
		
	}
}
